﻿using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Model;
using YouHP.IDAL;
using Microsoft.ApplicationBlocks.Data;
using System.Data;

namespace YouHP.SqlServerDAL
{
    public class RecommendUserDAL : IRecommendUser
    {

        #region 链接

        public bool RecommendUserAdd(RecommendUserInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.RecommendUserSP_RecommendUserAdd,
                info.username, info.diseasename);
            return cnt > 0;
        }

        public bool RecommendUserDel(int[] itemIds)
        {
            if (itemIds.Length == 0) return false;
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" [id] in ({0}", itemIds[0]);
            int cnt = itemIds.Length;
            for (int i = 1; i < cnt; i++)
            {
                sb.AppendFormat(",{0}", itemIds[i]);
            }
            sb.Append(") ");
            int cnt2 = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.RecommendUserSP_RecommendUserDel,
                sb.ToString());
            return cnt2 >= cnt;
        }

        public bool RecommendUserUpdate(RecommendUserInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.RecommendUserSP_RecommendUserUpdate,
                info.Id,info.username,info.diseasename);
            return cnt > 0;
        }

        public RecommendUserInfo RecommendUserGet(int id)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.RecommendUserSP_RecommendUserGet,
                id);
            return CBO.FillObject<RecommendUserInfo>(reader);
        }

/*
        public List<HyperLinkInfo> GetHyperLinkList()
        {
            return GetHyperLinkList("");
        }
        */
        private List<RecommendUserInfo> RecommendUserGetList(string condstr)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.RecommendUserSP_RecommendUserGetList,
                condstr);
            return CBO.FillCollection<RecommendUserInfo>(reader);

  
        }

        public List<RecommendUserInfo> RecommendUserGetList()
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                           Common.RecommendUserSP_RecommendUserGetList
                           ,"");

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                //count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                //count = 0;
            }
            dr = dset.Tables[0].CreateDataReader();

            return CBO.FillCollection<RecommendUserInfo>(dr);
        }
        #endregion

        #region 活跃用户
        public bool ActiveUserAdd(List<ActiveUserInfo> info)
        {
            int cnt = 0;
            int tmpcnt = 0;
            foreach (ActiveUserInfo item in info)
            {
                tmpcnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                                         Common.ActiveUserSP_ActiveUserAdd,
                                         item.userid,item.username,item.IsRecommended
                                      );
                cnt += tmpcnt;
            }
            return cnt>0;
        }
        public bool ActiveUserDel(int[] itemIds)
        {
            if (itemIds.Length == 0) return false;
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" [id] in ({0}", itemIds[0]);
            int cnt = itemIds.Length;
            for (int i = 1; i < cnt; i++)
            {
                sb.AppendFormat(",{0}", itemIds[i]);
            }
            sb.Append(") ");
            int cnt2 = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.ActiveUserSP_ActiveUserDel,
                sb.ToString());
            return cnt2 >= cnt;
        }
        public bool ActiveUserUpdate(ActiveUserInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.ActiveUserSP_ActiveUserUpdate,
                info.Id, info.username, info.IsRecommended);
            return cnt > 0;
        }
        public List<ActiveUserInfo> ActiveUserGetList()
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                           Common.ActiveUserSP_ActiveUserGetList
                           , "");

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                //count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                //count = 0;
            }
            dr = dset.Tables[0].CreateDataReader();

            return CBO.FillCollection<ActiveUserInfo>(dr);
        }
        public List<ActiveUserInfo> ActiveUserIntroGetList()
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                           Common.ActiveUserSP_ActiveUserIntroGetList
                           , "");

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                //count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                //count = 0;
            }
            dr = dset.Tables[0].CreateDataReader();

            return CBO.FillCollection<ActiveUserInfo>(dr);
        }
        #endregion

    }
}